/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bgu.sonar.util.ui.dialogs;

import bgu.sonar.util.ui.mon.Monitor;
import com.sun.awt.AWTUtilities;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

/**
 *
 * @author bennyl
 */
public class MonitoredPleaseWaitDialog extends javax.swing.JDialog {

    private Monitor monitor;
    private Timer timer;

    /** Creates new form MonitoredProgressDialog */
    public MonitoredPleaseWaitDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        setUndecorated(true);
        AWTUtilities.setWindowOpaque(this, false);
        initComponents();
        FlatDialogUtils.createDialogBackPanel(this, parent, false);
    }

    public void setModel(final Monitor monitor, String message) {
        this.monitor = monitor;
        this.titleLabel.setText(message);
        setPreferredSize(titleLabel.getPreferredSize());
        if (timer == null) {
            timer = new Timer(200, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (!isVisible()) { //dont start working before we are visible..
                        return;
                    }
                    if (monitor != null) {
                        double progress = monitor.getProgress();
                        progressbar.setValue((int) (progress * 100));
                        if (monitor.isClosed()) {
                            timer.stop();
                            setVisible(false);
                        }
                    } else {
                        timer.stop();
                        setVisible(false);
                    }
                }
            });
        }

        if (!timer.isRunning()) {
            timer.start();
        }
    }

    public static void show(Monitor mon) {
        MonitoredPleaseWaitDialog progress = new MonitoredPleaseWaitDialog(null, true);
        progress.setModel(mon, mon.getVisualName());
        progress.setLocationRelativeTo(null);
        progress.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        titleLabel = new javax.swing.JLabel();
        progressbar = new javax.swing.JProgressBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setModal(true);

        jPanel1.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(150, 150, 150)), javax.swing.BorderFactory.createLineBorder(new java.awt.Color(71, 71, 71), 5)));
        jPanel1.setLayout(new java.awt.BorderLayout());

        titleLabel.setBackground(new java.awt.Color(71, 71, 71));
        titleLabel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        titleLabel.setForeground(new java.awt.Color(255, 255, 255));
        titleLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/img/cog_go32.png"))); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(titleLabel, org.openide.util.NbBundle.getMessage(MonitoredPleaseWaitDialog.class, "MonitoredPleaseWaitDialog.titleLabel.text")); // NOI18N
        titleLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(71, 71, 71), 5));
        titleLabel.setOpaque(true);
        jPanel1.add(titleLabel, java.awt.BorderLayout.CENTER);

        progressbar.setBackground(new java.awt.Color(0, 0, 0));
        progressbar.setBorderPainted(false);
        progressbar.setPreferredSize(new java.awt.Dimension(146, 10));
        jPanel1.add(progressbar, java.awt.BorderLayout.PAGE_END);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MonitoredPleaseWaitDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MonitoredPleaseWaitDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MonitoredPleaseWaitDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MonitoredPleaseWaitDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                MonitoredPleaseWaitDialog dialog = new MonitoredPleaseWaitDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JProgressBar progressbar;
    private javax.swing.JLabel titleLabel;
    // End of variables declaration//GEN-END:variables
}
